﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class LevelSelectState : State
    {
        SpriteFont font;
        SpriteFont titleFont;
        private const float LEVEL_CHOICE_SEPERATION = 220f;
        private const float CHOICE_SEPERATION = 50f;

        // Background texture
        private Texture2D bg;

        // Bool shift titles down only once
        bool keyHeld = false;

        public LevelSelectState(SpriteFont newFont, SpriteFont newTitleFont, GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
            font = newFont;
            titleFont = newTitleFont;

            // List the applicable levels.
            addLevel("Moon", true);
            addLevel("Forest", true);
            addLevel("Volcano", true);

            selectedChoice = 0;

            bg = content.Load<Texture2D>("nebula_1");
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            enterPressed = false;
            base.onEnter(state);
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            int i;

            // Background
            spriteBatch.Begin();
            spriteBatch.Draw(bg, Vector2.Zero, Color.White);
            spriteBatch.End();

            // Draw Planets
            planets.DrawLevelPlanets();

            spriteBatch.Begin();

                // Display the background.
                //spriteBatch.Draw(background, screenSize, Color.White);
                spriteBatch.DrawString(titleFont, "LEVEL SELECT", new Vector2(screenSize.X / 2, 0), Color.White);

                // Display the choices.
                for (i = 0; i < choices.Count(); i++)
                {
                    if (access[i]) // The level can be accessed.
                    {
                        if (i == selectedChoice) // Is the current choice being printed.
                            spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, LEVEL_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Red);
                        else // The current choice is not being printed.
                            spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, LEVEL_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Yellow);
                    }
                    else
                        spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, LEVEL_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Gray);
                }
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
            if ((Keyboard.GetState().IsKeyDown(Keys.Up) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadUp)) && !keyHeld)
            {
                selectedChoice = (selectedChoice - 1) % choices.Count();

                if (selectedChoice < 0)
                    selectedChoice = choices.Count() - 1;

                // Key has been pressed
                keyHeld = true;
            }
            else if ((Keyboard.GetState().IsKeyDown(Keys.Down) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadDown)) && !keyHeld)
            {
                selectedChoice = (selectedChoice + 1) % choices.Count();

                // Key has been pressed
                keyHeld = true;
            }

            // Reset key press state
            if ((Keyboard.GetState().IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Down)) && (GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadUp) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadDown)))
            {
                keyHeld = false;
                enterPressed = false;
            }

            base.input(gameTime, keys, buttons, dpad, thumbSticks);
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            // Update Planets
            planets.UpdateLevelPlanets(gameTime);

            base.update(gameTime);
        }

        // Conditions necessary to enter go return to a previous state.
        public override bool goingBack(State next)
        {
            /*if (next is GamePlayState)
            {
                GamePlayState gNext = (GamePlayState)next;
                if (gNext.level.ReachedExit) // The exit has been reached.
                    return true;
                else
                    return false;
            }*/
            return false;
        }
        
        public void addLevel(string choice, bool canAccess)
        {
            choices.Add(choice);
            access.Add(canAccess);
        }

        // Conditions necessary to enter go to a next state.
        public override bool goingForward(State prev)
        {
            TitleState titleState = (TitleState)prev;

            if (titleState.keyHeld || !prev.enterPressed)
                return false;
            if (prev.enterPressed)
            {
                return true;
            }
            return false;
        }
    }
}